package com.ngc.mall.manager.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.Topic;

/**
 * redis键值通知配置类
 */
@Configuration
public class MessageListenerConfiguration {
    @Value("${spring.redis.pay-listen-pattern}")
    public String payPattern;

    @Value("${spring.redis.receive-listen-pattern}")
    public String receivePattern;

    @Value("${spring.redis.coupon_expire_listen_pattern}")
    public String couponExpirePattern;


    @Autowired
    private TopicMessageListener topicMessageListener;

    @Autowired
    private CouponExpireListener couponExpireListener;


    @Bean
    public RedisMessageListenerContainer orderPayListenerContainer(RedisConnectionFactory redisConnection) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(redisConnection);
        Topic topic = new PatternTopic(this.payPattern);

        container.addMessageListener(topicMessageListener, topic);
        return container;
    }

    @Bean
    public RedisMessageListenerContainer orderReceiveListenerContainer(RedisConnectionFactory redisConnection) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(redisConnection);
        Topic topic = new PatternTopic(this.receivePattern);
        container.addMessageListener(new ReceiveMessageListener(), topic);
        return container;
    }

    @Bean
    public RedisMessageListenerContainer couponExpireListenerContainer(RedisConnectionFactory redisConnection){
        RedisMessageListenerContainer container=new RedisMessageListenerContainer();
        container.setConnectionFactory(redisConnection);
        Topic topic=new PatternTopic(this.couponExpirePattern);
        container.addMessageListener(couponExpireListener,topic);
        return container;
    }
}
